package com.leostadyn.stackfarm.timestatistics.net.api;

import com.leostadyn.stackfarm.timestatistics.model.bean.AddProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.AddStaffBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.BaseResponse;
import com.leostadyn.stackfarm.timestatistics.model.bean.DeleteBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.DetailProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.EmployeeBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.JobBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.LoginBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.ProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.StaffTimeRecordBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.UpdateInfoBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.UserListBean;
import com.leostadyn.stackfarm.timestatistics.net.base.HttpService;
import com.leostadyn.stackfarm.timestatistics.net.base.RxSchedulers;
import com.leostadyn.stackfarm.timestatistics.net.service.AdminService;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;

public class AdminApi {
    private static volatile AdminApi adminApi = null;
    private AdminService adminService;

    public AdminApi() {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        adminService = HttpService.initialize().create(AdminService.class);
    }

    public static AdminApi getInstance() {
        if (adminApi == null) {
            synchronized (AdminApi.class) {
                if (adminApi == null) {
                    adminApi = new AdminApi();
                }
            }
        }
        return adminApi;
    }

    public Observable<BaseResponse<LoginBean>> login(String account, String password) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getLoginCall(account, password).compose(RxSchedulers.io_main());
    }

    //增加人员
    public Observable<BaseResponse<AddStaffBean>> addStaff(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.addStaff(paramer)
                .compose(RxSchedulers.io_main());
    }

    public Observable<BaseResponse<List<EmployeeBean>>> getEmployeeInformation() {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getEmployeeCall().compose(RxSchedulers.io_main());
    }

    public Observable<BaseResponse<List<DetailProjectBean>>> getProjectInformation(String end, String start) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getProjectCall(end, start, 1).compose(RxSchedulers.io_main());
    }

    public Observable<BaseResponse<List<DetailProjectBean>>> getPersonnelDetails(String end, String start, String uid) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getPersonnelDetail(end, start, uid).compose(RxSchedulers.io_main());
    }

    public Observable<BaseResponse<List<EmployeeBean>>> getProjectDetail(String end, String start, String projectCode) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getProjectDetail(end, start, projectCode).compose(RxSchedulers.io_main());
    }

    //管理员修改密码
    public Observable<BaseResponse<UpdateInfoBean>> setPassword(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.setPassword(paramer).compose(RxSchedulers.io_main());
    }

    //编辑信息
    public Observable<BaseResponse<UpdateInfoBean>> updateInfo(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.updateInfo(paramer).compose(RxSchedulers.io_main());
    }

    //人员列表
    public Observable<BaseResponse<List<UserListBean>>> getStaffList(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getStaffRecord(paramer).compose(RxSchedulers.io_main());
    }

    //删除
    public Observable<BaseResponse<DeleteBean>> deleteData(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.deleteData(paramer).compose(RxSchedulers.io_main());
    }

    //设置级的修改密码
    public Observable<BaseResponse<UpdateInfoBean>> modifyPassword(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.modifyPassword(paramer).compose(RxSchedulers.io_main());
    }

    //项目的列表
    public Observable<BaseResponse<List<ProjectBean>>> getProjectList(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getProjectRecord(paramer).compose(RxSchedulers.io_main());

    }

    //添加项目
    public Observable<BaseResponse<AddProjectBean>> addProject(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.addProject(paramer).compose(RxSchedulers.io_main());
    }

    //编辑项目
    public Observable<BaseResponse<UpdateInfoBean>> updateProjectInfo(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.updateProjectInfo(paramer).compose(RxSchedulers.io_main());
    }

    //删除项目
    public Observable<BaseResponse<DeleteBean>> deleteProjectData(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.deleteProject(paramer).compose(RxSchedulers.io_main());
    }

    //根据用户获得工时列表
    public Observable<BaseResponse<StaffTimeRecordBean>> getTimeRecord(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getTimeRecord(paramer).compose(RxSchedulers.io_main());
    }

    //删除一条工时记录
    public Observable<BaseResponse<DeleteBean>> deleteTimeRecord(HashMap<String, String> paramer) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.deleteTimeRecord(paramer).compose(RxSchedulers.io_main());
    }

    public Observable<BaseResponse<String>> addTimeRecord(String projectId, float taskTime, String workDate) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.addTimeRecord(projectId, taskTime, workDate).compose(RxSchedulers.io_main());
    }

    public Observable<BaseResponse<String>> updateTimeRecord(String timesheetId, String projectId, float taskTime, String workDate) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.updateTimeRecord(timesheetId, projectId, taskTime, workDate).compose(RxSchedulers.io_main());
    }

    //提交一周工时
    public Observable<BaseResponse<String>> submitTimeRecord(String ids) {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.submitTime(ids).compose(RxSchedulers.io_main());
    }

    //获取工作岗位
    public Observable<BaseResponse<List<JobBean>>> getJob() {
        HttpService.initialize().timeOut(HttpService.WRITETIMEOUT);
        return adminService.getJob().compose(RxSchedulers.io_main());
    }


}
